﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Lab1
{
    public class QuestionService
    {
        private DatabaseEntities db = new DatabaseEntities();
        public IEnumerable<Question> getAll()
        {
            List<Question> list = (from t in db.Question
                                   select t).ToList();

            return list;
        }
        public void update(Question model)
        {

            using (DatabaseEntities context = new DatabaseEntities())
            {
                var original = (from t in context.Question
                                where t.Id.Equals(model.Id)
                                select t).SingleOrDefault();
                context.Question.ApplyCurrentValues(model);
                context.SaveChanges();
            }

        }
        public void delete(Question model)
        {
            using (DatabaseEntities context = new DatabaseEntities())
            {
                var original = (from t in context.Question
                                where t.Id.Equals(model.Id)
                                select t).SingleOrDefault();
                context.DeleteObject(original);
                context.SaveChanges();
            }
        }
        public void save(Question model)
        {
            db.AddToQuestion(model);
            db.SaveChanges();
        }

        public Question getRoot()
        {
            try
            {
                return (from t in db.Question
                        where t.Root == true
                        select t).Single<Question>();
            }
            catch
            {
                return null;
            }
        }
        public Question getById(Guid id)
        {
            Question model = (from t in db.Question
                              where t.Id.Equals(id)
                              select t).Single<Question>();

            return model;
        }
    }
}
